package com.clz.latte.ec.sign;

import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.util.Patterns;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.clz.latte.delegate.LatteDaleGate;
import com.clz.latte.ec.R;
import com.clz.latte.ec.R2;
import com.clz.latte.ec.entity.User;
import com.clz.latte.net.RestClient;
import com.clz.latte.net.callback.IError;
import com.clz.latte.net.callback.IFailure;
import com.clz.latte.net.callback.ISuccess;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/1/3.
 * 登录
 */

public class SignInDelegate extends LatteDaleGate {
    @BindView(R2.id.edit_email)
    TextInputEditText mEditEmail;
    @BindView(R2.id.edit_pwd)
    TextInputEditText mEditPwd;
    @BindView(R2.id.tv_link_sign_up)
    TextView mTvLinkSignUp;
    private ISignListener mISignListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ISignListener) {
            mISignListener = (ISignListener) activity;
        }
    }

    @OnClick(R2.id.btn_sign_in)
    void onSignInClicked() {
        if (checkForm()) {
            RestClient.builder()
                    .url("http://192.168.1.107:82/RestServer/data/user_profile.json")
                    .params("email", mEditEmail.getText().toString())
                    .sucess(new ISuccess() {
                        @Override
                        public void onSucess(String response) {
                            Gson gson = new Gson();
                            final User user = gson.fromJson(response, User.class);
                            SignHandler.onSignIn(user, mISignListener);
                        }
                    })
                    .error(new IError() {
                        @Override
                        public void onError(int code, String message) {

                        }
                    })
                    .failure(new IFailure() {
                        @Override
                        public void onFailure() {

                        }
                    })
                    .build().post();
        }
    }

    @OnClick(R2.id.tv_link_sign_up)
    void onLinkSignUpClicked() {
        start(new SignUpDelegate(), SINGLETASK);
    }

    @OnClick(R2.id.icon_sign_in_wx)
    void onIconSignInWxClicked() {

        Toast.makeText(getActivity(), "微信登录", Toast.LENGTH_SHORT).show();

    }

    @Override
    public Object setLayOut() {
        return R.layout.delegate_sign_in;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvLinkSignUp.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        mTvLinkSignUp.getPaint().setAntiAlias(true);//抗锯齿
    }

    /**
     * 检测登录信息合法性
     *
     * @return
     */
    private boolean checkForm() {
        boolean isPass = true;
        final String email = mEditEmail.getText().toString().trim();
        final String pwd = mEditPwd.getText().toString().trim();
        if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            mEditEmail.setError("错误的邮箱格式");
            isPass = false;
        } else {
            mEditEmail.setError(null);
        }

        if (pwd.isEmpty() || pwd.length() < 6) {
            mEditPwd.setError("密码长度不能小于6位");
            isPass = false;
        } else {
            mEditPwd.setError(null);
        }
        return isPass;
    }
}
